<?php

namespace App\Services\Admin;

use App\Models\UserSubscribe;
use App\Models\UserTrain;

class Index extends Base
{
    public static function index()
    {
        $input = request()->all();

        $arr = Reservation::lists()->orderBy('class_time', 'asc')->get()->toArray();

        $time_order = [];

        foreach ($arr as $k => $time_v) {
            $arr[$k]['time_order'] = $time_v['class_time']['time_start'];
            $time_order[] = $time_v['class_time']['time_start'] ?? '';
            $arr[$k]['teacher'] = Reservation::class_bass_info_teacher_arr($time_v['class_base_info_teacher']);
        }

        //排序
        array_multisort($time_order, SORT_ASC, SORT_NUMERIC, $arr);

        //查询预约数
        $class_base_info_id = array_column($arr, 'id');

        //预约总人数
        $class_subscribe_number = Reservation::class_subscribe_number($class_base_info_id);

        //预约成功
        $appointment_successful = Reservation::class_subscribe_number($class_base_info_id,[2]);

        //预约失败
        $appointment_failed = Reservation::class_subscribe_number($class_base_info_id,[4]);

        //预约排队
        $appointment_appointment = Reservation::class_subscribe_number($class_base_info_id,[1]);

        foreach ($arr as $k => &$v) {

            $open_an_appointment = Reservation::current_control_time($v['class_base_info_suspension'], $input['day']);

            if($open_an_appointment == 0) {
                //全局控制是否开启预约
                if ($v['is_display'] == 1) {
                    $v['is_display_name'] = '是';
                } else {
                    $v['is_display_name'] = '否';
                }
            }else{
                //部分控制是否开启预约
                if ($open_an_appointment == 1) {
                    $v['is_display_name'] = '是';
                } else {
                    $v['is_display_name'] = '否';
                }
            }

            $v['class_subscribe_number'] = $class_subscribe_number[$v['id']] ?? 0;

            $v['appointment_successful'] = $appointment_successful[$v['id']] ?? 0;

            $v['appointment_failed'] = $appointment_failed[$v['id']] ?? 0;

            $v['appointment_appointment'] = $appointment_appointment[$v['id']] ?? 0;

        }

        $result = [
            'classroom_lists' => Classroom::lists()->get(),
            'arr' => array_values($arr),
            'user_rank_id' => request()->get('user_rank_id'),
            'user_rank'=>UserRankExp::level()
        ];

        return $result;
    }

    /**
     * 签到列表user_train
     * @param int $id
     * @param int $date
     * @return mixed
     */
    public static function user_train($id = 0, $date = 0)
    {

        $user_train = (new UserTrain());

        if (!empty($date)) {
            $start_date = "{$date} 00:00:00";
            $end_date = "{$date} 23:59:59";

            $user_train = $user_train->whereBetween('created_at', [$start_date, $end_date]);
        }

        return $user_train->whereHas('class_base_info', function ($query) use ($id) {
            if (!empty($id)) {
                $query->where('id', $id);
            }
        });
    }
}
